<!--
  弹框组件       zls-2022/2/10
      Attributes
          1、title          弹框标题名称
          2、btns           操作按钮数组
          其他所有属性请参照 Popup.vue 公共组件

          参数解析==》
              1、btns操作按钮数组
                  [
                    {
                      text: '取消',
                      type: 1,
                      event: 'cancel'
                    },
                    {
                      text: '确定',
                      type: 2,
                      event: 'sure'
                    }
                  ]

      Events
          1、close          关闭弹框函数

      Demo==>
          <div style="margin-bottom: 20px">
            <el-button type="primary" plain @click="show = true">定制弹框</el-button>
            <z-modal title="弹框标题" :show="show" :width="500" :height="300" @close="show = false" @cancel="show = false" @sure="sure">
              <div class="modal-main" style="height: 100%">
                <div style="height: 1500px">请开始你的表演</div>
              </div>
            </z-modal>
          </div>

          import { ref } from 'vue'
          const show = ref<boolean>(false)
          function sure() {}
-->
<template>
  <div class="z-modal">
    <z-popup :height="$attrs.height" position="center" :round="$attrs.round" :show="$attrs.show" :width="$attrs.width" :zIndex="$attrs.zIndex" @close="close">
      <div class="z-modal-main">
        <div class="z-modal-main-title">
          <span>{{ title }}</span>
          <i class="close" @click.stop="close"></i>
        </div>
        <div :style="{ height: btns.length ? 'calc(100% - 110px)' : 'calc(100% - 50px)' }" class="z-modal-main-content scrollbar">
          <slot></slot>
        </div>
        <ul v-if="btns.length" class="z-modal-main-btn">
          <li v-for="(btnItem, btnI) in btns" :key="btnI" :style="{ backgroundColor: btnItem.bgColor }" class="btnRipple" @click="btnClick(btnItem.event)">
            {{ btnItem.text }}
          </li>
        </ul>
      </div>
    </z-popup>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  name: 'z-modal',
  inheritAttrs: false,
  props: {
    title: {
      type: String,
      default: ''
    },
    btns: {
      type: Array as () => Array<any>,
      default: () => [
        {
          text: '取消',
          type: 1,
          event: 'cancel'
        },
        {
          text: '确定',
          type: 2,
          event: 'sure'
        }
      ]
    }
  },
  setup(props, { emit }) {
    init()

    function init() {
      if (props.btns.length) {
        props.btns.forEach(v => {
          v.bgColor = v.type === 1 ? '#a6a9ad' : v.type === 2 ? 'var(--el-color-primary)' : ''
        })
      }
    }

    function close() {
      emit('close')
    }

    function btnClick(val: string) {
      emit(val)
    }

    return { close, btnClick }
  }
})
</script>

<style lang="scss" scoped>
.z-modal {
  &-main {
    width: 100%;
    height: 100%;
    &-title {
      height: 50px;
      text-align: center;
      line-height: 50px;
      position: relative;
      cursor: grabbing;
      color: #fff;
      background-image: linear-gradient(90deg, var(--el-color-primary), var(--el-color-primary-light-3), var(--el-color-primary));
      &:after {
        content: '';
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 1px;
        background-image: linear-gradient(90deg, #053556, #05849c, #053556);
      }
      > span {
        font-size: 20px;
        font-weight: 900;
        letter-spacing: 6px;
        user-select: none;
      }
      .close {
        position: absolute;
        right: 15px;
        top: 13px;
        width: 20px;
        height: 20px;
        cursor: pointer;
        &:before,
        &:after {
          position: absolute;
          content: ' ';
          height: 20px;
          width: 2px;
          border-radius: 2px;
          background-color: #fff;
        }
        &:before {
          transform: rotate(45deg);
        }
        &:after {
          transform: rotate(-45deg);
        }
      }
    }
    &-content {
      width: 100%;
      padding: 15px 15px 0 15px;
      // height: calc(100% - 130px);
      overflow: auto;
    }
    &-btn {
      height: 60px;
      @include flex(center, center);
      > li {
        width: 100px;
        height: 32px;
        margin: 0 10px;
        font-size: 16px;
        font-weight: 900;
        color: #fff;
        letter-spacing: 4px;
        border-radius: 3px;
        cursor: pointer;
        user-select: none;
        @include flex(center, center);
      }
    }
  }
}
</style>
